home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / traceroute-1.0.lha / traceroute / src / socket_autoinit.c < prev    next >
C/C++ Source or Header  |  1994-05-25  |  4KB  |  145 lines

  1. static char *RCS_ID_C="$Id: autoinit.c,v 1.10 1993/10/18 05:53:01 jraja Exp $";
  2. /*
  3.  * autoinit.c --- SAS C auto initialization functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  * Modified: kjk <klei0001@uni-trier.de>
  7.  *
  8.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  9.  *                  Helsinki University of Technology, Finland.
  10.  *                  All rights reserved.
  11.  *
  12.  * Created      : Sat Mar 20 03:31:29 1993 ppessi
  13.  * Last modified: Wed May 25 17:51:41 1994 kjk
  14.  *
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/libraries.h>
  19.  
  20. #include <intuition/intuition.h>
  21.  
  22. #include <proto/socket.h>
  23. #include <proto/exec.h>
  24. #include <proto/intuition.h>
  25.  
  26. #include <stdlib.h>
  27.  
  28. struct Library *SocketBase = NULL;
  29.  
  30. int errno = 0;                  /* global errno variable */
  31.  
  32. STRPTR SOCKETNAME = "bsdsocket.library";
  33.  
  34. #define SOCKETVERSION 2         /* minimum bsdsocket version to use */
  35.  
  36. extern STRPTR _ProgramName;     /* SAS startup module defines this :-) */
  37.  
  38. /****** net.lib/autoinit *********************************************
  39. *
  40. *   NAME   
  41. *       autoinit - SAS C Autoinitialization Functions 
  42. *
  43. *   SYNOPSIS
  44. *       _STIopenSockets()
  45. *
  46. *       void _STIopenSockets(void)
  47. *
  48. *       _STDcloseSockets()
  49. *
  50. *       void _STDcloseSockets(void)
  51. *
  52. *   FUNCTION
  53. *       These functions open and close the bsdsocket.library at the
  54. *       startup and exit of the program, respectively. For a
  55. *       program to use these functions, it must be linked with
  56. *       netlib:net.lib.
  57. *
  58. *       If the library can be opened, the _STIopenSockets() calls
  59. *       bsdsocket.library function SetErrnoPtr() to tell the
  60. *       library the address and the size of the errno variable of
  61. *       the calling program. 
  62. *
  63. *   NOTES
  64. *       _STIopenSockets() also checks that the system version is at
  65. *       least 37. It puts up a requester if the bsdsocket.library
  66. *       is not found or is of wrong version.
  67. *
  68. *       The autoinitialization and autotermination functions are
  69. *       features specific to the SAS C6. However, these functions
  70. *       can be used with other (ANSI) C compilers, too. Example
  71. *       follows: 
  72. *
  73. *       \* at start of main() *\
  74. *
  75. *       atexit(_STDcloseSockets);
  76. *       _STDopenSockets();
  77. *
  78. *   BUGS
  79. *
  80. *   SEE ALSO
  81. *       bsdsocket.library/SetErrnoPtr(),
  82. *       SAS/C 6 User's Guide p. 145 for details of
  83. *       autoinitialization and autotermination functions.  
  84. *****************************************************************************
  85. *
  86. */
  87.  
  88. /*
  89.  * Using __stdargs prevents creation of register arguments entry point.
  90.  * If both stack args and reg. args entry points are created, this
  91.  * function is called _twice_, which is not wanted.
  92.  */
  93.  __stdargs
  94. int _STIopenSockets(void)
  95. {
  96.   struct Library *IntuitionBase;
  97.   STRPTR errorStr;
  98.  
  99.   /*
  100.    * Check OS version
  101.    */
  102.   if ((*(struct Library **)4)->lib_Version < 37)
  103.     return(-1); /* well, in case of failure, anything but 0 may be returned */
  104.  
  105.   /*
  106.    * Open bsdsocket.library
  107.    */
  108.   if ((SocketBase = OpenLibrary(SOCKETNAME, SOCKETVERSION)) != NULL) {
  109.     /*
  110.      * Succesfull. Now tell bsdsocket.library the address of our errno
  111.      */
  112.     SetErrnoPtr(&errno, sizeof(errno));
  113.     
  114.     return(0);
  115.   }
  116.   else
  117.     errorStr = "AmiTCP/IP version 2 or later must be started first.";
  118.   
  119.   IntuitionBase = OpenLibrary("intuition.library", 36);
  120.   
  121.   if (IntuitionBase != NULL) {
  122.     struct EasyStruct libraryES;
  123.     
  124.     libraryES.es_StructSize = sizeof(libraryES);
  125.     libraryES.es_Flags = 0;
  126.     libraryES.es_Title = _ProgramName;
  127.     libraryES.es_TextFormat = errorStr;
  128.     libraryES.es_GadgetFormat = "Exit %s";
  129.     
  130.     EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
  131.  
  132.     CloseLibrary(IntuitionBase);
  133.   }
  134.   return(-1);
  135. }
  136.  
  137. void __stdargs
  138. _STDcloseSockets(void)
  139. {
  140.   if (SocketBase) {
  141.     CloseLibrary(SocketBase);
  142.     SocketBase = NULL;
  143.   }
  144. }
  145.